package com.htd.htdapmservice.service.impl;

import com.htd.htdapmcommon.constants.AlarmConstants;
import com.htd.htdapmcommon.util.RedisUtil;
import com.htd.htdapmdmo.OneAlertKey;
import com.htd.htdapmservice.dao.OneAlertKeyDao;
import com.htd.htdapmservice.service.intf.OneAlertKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OneAlertKeyServiceImpl implements OneAlertKeyService {
    @Autowired
    private OneAlertKeyDao oneAlertKeyDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public int addOneAlertKey(OneAlertKey oneAlertKey) {
        String key = "APM_"+oneAlertKey.getProductCode()+"_KEY";
        if (!redisUtil.exists(key)){
            redisUtil.set(key,oneAlertKey.getProductKeyt(), AlarmConstants.ONE_ALERT_TOKEN_EXPIRE);
        }
        return oneAlertKeyDao.addOneAlertKey(oneAlertKey);
    }

    @Override
    public int delOneAlertKeyByProductCode(String productCode) {
        String key = "APM_"+productCode+"_KEY";
        if (!redisUtil.exists(key)){
            redisUtil.delete(productCode);
        }
        return oneAlertKeyDao.delOneAlertKeyByProductCode(productCode);
    }

    @Override
    public int updataOneAlertKeyByProductCode(Map<String,Object> map) {
        return oneAlertKeyDao.updataOneAlertKeyByProductCode(map);
    }

    @Override
    public OneAlertKey findOneAlertKeyByProductCode(String productCode) {
        return oneAlertKeyDao.findOneAlertKeyByProductCode(productCode);
    }

    public List<OneAlertKey> findOneAlertKeyList(){
        return oneAlertKeyDao.findOneAlertKeyList();
    }
}
